/*
   Copyright 2007 Jeremy Wales

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.googlecode.cpbi.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleListener;
import org.osgi.framework.Constants;
import org.springframework.util.StringUtils;

/**
 * @author Jeremy Wales
 */
public class DefaultOsgi implements Osgi {
	static final Log LOG = LogFactory.getLog(DefaultOsgi.class);

	private final BundleContext bundleContext;

	public DefaultOsgi(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
	}

	public String[] getBootDelegationPackagePatterns() {
		String bootDelegationValue = bundleContext.getProperty(Constants.FRAMEWORK_BOOTDELEGATION);
		return StringUtils.addStringToArray(StringUtils.commaDelimitedListToStringArray(bootDelegationValue), "java.*");
	}

	public void register(BundleListener bundleListener) {
		bundleContext.addBundleListener(bundleListener);
	}

	public final Bundle tryToInstallBundleFrom(String bundleLocation) {
		try {
			return installBundleFrom(bundleLocation);
		} catch (Exception e) {
			LOG.error("Could not install bundle from '" + bundleLocation + "'.", e);
			return UNINSTALLED_BUNDLE;
		}
	}

	public Bundle installBundleFrom(String bundleLocation) throws IOException {
		return installBundleFrom(FileUtils.getInputStreamFrom(bundleLocation), bundleLocation);
	}

	public Bundle installBundleFrom(InputStream bundleInputStream, String location) {
		try {
			return bundleContext.installBundle(FileUtils.decode(location), bundleInputStream);
		} catch (BundleException e) {
			throw new CpbiException("Could not install bundle '" + location + "'.", e);
		}
	}

	public void updateSystemBundle() {
		Bundle systemBundle = bundleContext.getBundle(0);
		try {
			systemBundle.update();
		} catch (BundleException e) {
			throw new CpbiException("Could not restart System Bundle.", e);
		}
	}

	public void tryToStart(Bundle bundle) {
		try {
			bundle.start();
		} catch (BundleException e) {
			LOG.error("Could not start bundle '" + bundle.getLocation() + "'.", e);
		}
	}

	public Bundle installEmptyBundleAround(Manifest manifest, String location) {
		try {
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest);
			jarOutputStream.close();
			return installBundleFrom(new ByteArrayInputStream(outputStream.toByteArray()), location);
		} catch (IOException e) {
			throw new CpbiException("Could not create bundle around mainfest: '" + new BundleManifest(manifest) + "'.",
					e);
		}
	}
}
